﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using OnlineRailway.Administrator;
using OnlineRailway.EF;


namespace OnlineRailway.UI.Administrator
{
    public partial class ScheduleManagement : System.Web.UI.Page
    {
        private SqlCommand cmd;
        private SqlDataAdapter dataAdapter;
        private DataSet ds= new DataSet();
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Text = "";
            if (!IsPostBack)
            {
                BindSchedule();
            }
        }
        void BindSchedule()
        {
            try
            {
                cmd = new SqlCommand("SELECT [ScheduleID],[ScheduleName],[StartStation],[EndStation],[BetweenStation],[Distances],[LongTime],[Fare],[Description],[CreateDate]=convert(varchar,CreateDate,110),[Status] FROM TrainSchedules", Connect.Conn);
                if (Connect.Conn.State==ConnectionState.Closed)
                {
                    Connect.Conn.Open();
                }
                dataAdapter = new SqlDataAdapter(cmd);
                dataAdapter.Fill(ds);
                if (ds.Tables[0].Rows.Count>0)
                {
                    gvSchedule.DataSource = ds;
                    gvSchedule.DataBind();

                }
                else
                {
                    ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                    gvSchedule.DataSource = ds;
                    gvSchedule.DataBind();
                    int columncount = gvSchedule.Rows[0].Cells.Count;
                    gvSchedule.Rows[0].Cells.Clear();
                    gvSchedule.Rows[0].Cells.Add(new TableCell());
                    gvSchedule.Rows[0].Cells[0].ColumnSpan = columncount;
                    gvSchedule.Rows[0].Cells[0].Text = "No Records Found";
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Error Bind: " + ex.Message;
            }
            finally
            {
                Connect.Conn.Close();
            }
        }

        protected void gvSchedule_RowEditing(object sender, GridViewEditEventArgs e)
        {
            
        }

        protected void gvSchedule_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvSchedule.EditIndex = -1;
            BindSchedule();
        }

        protected void gvSchedule_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = Convert.ToInt32(gvSchedule.DataKeys[e.RowIndex]["ScheduleID"]);
            var name = gvSchedule.DataKeys[e.RowIndex]["ScheduleName"];
            using (var context = new RailwayOnlineEntities())
            {
                var obj = context.TrainSchedules.First(x => x.ScheduleID == id);
                context.TrainSchedules.DeleteObject(obj);
                context.SaveChanges();
                lblMessage.Text = "Schedule name " + name + " has been deleted!";
            }
            BindSchedule();
        }

        protected void gvSchedule_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType==DataControlRowType.DataRow)
            {
                var name = DataBinder.Eval(e.Row.DataItem, "ScheduleName");
                var ibtn = e.Row.FindControl("ibtnDelete") as ImageButton;
                if (ibtn!=null)
                {
                    ibtn.Attributes.Add("onclick", "javascript:return ConfirmationBox('"+name+"')");
                }
            }
        }

        protected void gvSchedule_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Response.Redirect("ScheduleUpdate.aspx?id=" + gvSchedule.DataKeys[e.RowIndex]["ScheduleID"]);
        }
    }
}